<template>
  <div>
    <!-- 弹出层的头部区域 -->
    <van-nav-bar title="频道管理">
      <template #right>
        <van-icon
          name="cross"
          size="0.37333334rem"
          color="white"
          @click="closeFn"
        />
      </template>
    </van-nav-bar>
    <!-- 我的频道 -->
    <div class="my-channel-box">
      <div class="channel-title">
        <span
          >我的频道
          <span class="small-title">
            点击{{ isShow ? "删除" : "进入" }}频道
          </span>
        </span>
        <span @click="editFn">{{ isShow ? "完成" : "编辑" }}</span>
      </div>
      <!-- 我的频道列表 -->
      <van-row type="flex">
        <van-col span="6" v-for="item in userChannelList" :key="item.id" @click="myChannelFn(item)">
          <div class="channel-item van-hairline--surround">
            {{ item.name }}
            <!-- 删除的徽标 -->
            <van-badge
              color="transparent"
              class="cross-badge"
              v-show="isShow && item.id !== 0"
            >
              <template #content>
                <van-icon
                  name="cross"
                  class="badge-icon"
                  color="#cfcfcf"
                  size="0.32rem"
                />
              </template>
            </van-badge>
          </div>
        </van-col>
      </van-row>
    </div>

    <!-- 更多频道 -->
    <div class="more-channel-box">
      <div class="channel-title">
        <span>点击添加更多频道：</span>
      </div>
      <!-- 更多频道列表 -->
      <van-row type="flex">
        <van-col span="6" v-for="item in unChannelList" :key="item.id" @click="addMoreFn(item)">
          <div class="channel-item van-hairline--surround">{{ item.name }}</div>
        </van-col>
      </van-row>
    </div>
  </div>
</template>

<script>
import { allChannelListAPI, uploadUserChannelAPI } from '@/api/index'
export default {
  props: {
    userChannelList: {
      type: Array,
      default: () => {}
    }
  },
  data () {
    return {
      allChannelList: [],
      isShow: false // 控制编辑图标的文字切换
    }
  },
  created () {
    this.getAllChannelsFn()
  },
  methods: {
    async getAllChannelsFn () {
      const res = await allChannelListAPI()
      this.allChannelList = res.data.data.channels
    },
    // 点击关闭弹窗
    closeFn () {
      this.isShow = false
      this.$emit('input', false)
    },
    // 添加用户已选频道
    addMoreFn (obj) {
      // 只有处于编辑状态时才能执行添加操作
      // props本身是只读的，也就是说你不可以给变量重新赋值变量名=值
      // 数组里添加元素，也会影响到父级的这个数组的(eslint不让我改props里变量，所以我就传给父级，让父级改完在给我流回来-遵守单向数据流)
      if (this.isShow) {
        const arr = [...this.userChannelList]
        arr.push(obj)
        this.$emit('update:userChannelList', arr) // 依赖双向数据绑定.sync快速实现自定义事件名用法
      }
      // ·sync和父传子是一个异步的操作（后续想要使用props最新的值，需要等待DOM更新后数据流回来)
      // this.$nextTick(() => { // 等待数据变化后真实DOM更新以后，再触发此回调函数
      //   this.uploadUserChannelFn()
      // })
    },
    // 点击完成或者编辑
    editFn () {
      // console.log(this.isShow)
      // 当点击完成的时候更新用户已选频道数据
      if (this.isShow === true) {
        this.uploadUserChannelFn()
      }
      this.isShow = !this.isShow
    },
    // 更新用户选择频道
    async uploadUserChannelFn () {
      // 处理传入接口的数据格式
      const newArr = this.userChannelList.map((obj, index) => {
        return {
          id: obj.id,
          seq: index // 频道索引前端自己排序即可
        }
      })
      await uploadUserChannelAPI({
        channels: newArr
      })
    },
    // 我的频道--点击事件、点击删除用户已选频道
    myChannelFn (channelObj) {
      if (this.isShow === true && channelObj.id !== 0) {
        const arr = [...this.userChannelList]
        // 找到用户删除的频道索引
        const index = arr.findIndex(item => item.id === channelObj.id)
        arr.splice(index, 1)
        this.$emit('update:userChannelList', arr) // 通知父组件，给用户频道进行重新赋值
      } else {
        this.$emit('changeChannel', channelObj)
        this.$emit('input', false)// 关闭弹窗
      }
    }
  },
  computed: {
    unChannelList () {
      const newArr = []
      this.allChannelList.forEach((item) => {
        const targetObj = this.userChannelList.find((obj) => {
          return item.id === obj.id
        })
        if (targetObj === undefined) {
          newArr.push(item)
        }
      })
      return newArr
    }
  }
}
</script>

<style scoped lang="less">
.van-popup,
.popup-container {
  background-color: transparent;
  height: 100%;
  width: 100%;
}

.popup-container {
  display: flex;
  flex-direction: column;
}

.pop-header {
  height: 90px;
  background-color: #007bff;
  color: white;
  text-align: center;
  font-size: 14px;
  position: relative;
  .title {
    width: 100%;
    position: absolute;
    bottom: 15px;
  }
}

.pop-body {
  flex: 1;
  overflow: scroll;
  padding: 8px;
  background-color: white;
}

.my-channel-box,
.more-channel-box {
  .channel-title {
    display: flex;
    justify-content: space-between;
    font-size: 14px;
    line-height: 28px;
    padding: 0 6px;
  }
}

.channel-item {
  font-size: 12px;
  text-align: center;
  line-height: 36px;
  background-color: #fafafa;
  margin: 5px;
}

/*删除的微标 */
.cross-badge {
  position: absolute;
  right: -3px;
  top: 0;
  border: none;
}

/*提示文字 */
.small-title {
  font-size: 10px;
  color: gray;
}
</style>
